就是一个简单的下拉框的实现
package com.example.administrator.bbb; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView; import java.util.ArrayList; /** * Created by Administrator on 2017/3/29. */ public class SpinnerActivity extends Activity { Spinner spinner; TextView textView; ArrayList<String> arrayList = new ArrayList<String>(); ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_spinner); spinner = (Spinner)findViewById(R.id.spinner_City); textView = (TextView)findViewById(R.id.TextView_Show); //1定义数据类型,设置显示的数据 arrayList.add("北京"); arrayList.add("上海"); arrayList.add("南京"); arrayList.add("天津"); arrayList.add("合肥"); arrayList.add("哈尔滨"); arrayList.add("长沙"); arrayList.add("杭州"); arrayList.add("沈阳"); //2为下拉列表建一个适配器,用ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,arrayList); //设置下拉列表的菜单样式 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //3将适配器加载到sipnner上 spinner.setAdapter(adapter); //4加点击事件 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { textView.setText("你选择的是:" adapter.getItem(position)); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); }}
评论